package com.example.springsecurity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springsecurity.common.enums.DelStatusEnums;
import com.example.springsecurity.common.enums.MenuTypeEnums;
import com.example.springsecurity.common.req.SystemMenuReq;
import com.example.springsecurity.entity.SystemMenu;
import com.example.springsecurity.mapper.SystemMenuMapper;
import com.example.springsecurity.service.SystemMenuService;
import com.example.springsecurity.common.vo.SystemMenuListVo;
import com.example.springsecurity.common.vo.login.SystemMenuTreeVo;
import com.example.springsecurity.common.vo.login.SystemMenuVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.example.springsecurity.common.CommonMethod.*;
import static com.example.springsecurity.common.constant.Constant.PERMISSION_PREFIX;
import static com.example.springsecurity.common.utils.SecurityUtil.getSystemUserName;

/**
 * 菜单信息表(SystemMenu)表服务实现类
 *
 * @author aman
 * @since 2025-01-15 16:59:00
 */
@Service
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> implements SystemMenuService {

    /**
     * @Description：用户登陆成功后，获取系统菜单信息
     * @Author：fengcheng
     * @Date：2025/1/17 10:18
     * @ParamsType：[]
     * @Return：java.util.List<com.example.springsecurity.vo.login.SystemMenuTreeVo>
     */
    @Override
    public List<SystemMenuTreeVo> getMenuList() {
        List<SystemMenu> list = getBaseQueryWrapper();
        List<SystemMenuVo> menuVOS = convertSysResourceToMenuVO(list);
        return buildSystemMenuTreeVo(menuVOS);
    }

    /**
     * @Description：查询菜单列表
     * @Author：fengcheng
     * @Date：2025/1/17 10:19
     * @ParamsType：[]
     * @Return：java.util.List<com.example.springsecurity.vo.SystemMenuListVo>
     */
    @Override
    public List<SystemMenuListVo> queryMenuList() {
        List<SystemMenu> list = getBaseQueryWrapper();
        List<SystemMenuListVo> systemMenuVos = convertSysResourceToMenuListVO(list);
        Map<String, SystemMenuListVo> map = new HashMap<>();
        // 先将所有资源按照父节点ID进行分组
        for (SystemMenuListVo menuVo : systemMenuVos) {
            menuVo.setChildren(new ArrayList<>());
            // 如果是根节点则直接加入结果列表
            if (menuVo.getParentId() == 0) {
                map.put(menuVo.getId(), menuVo);
            } else {
                // 如果不是根节点，则加入父节点的 children 列表
                SystemMenuListVo parent = map.get(menuVo.getParentId().toString());
                if (ObjectUtils.isNotEmpty(parent)) {
                    parent.getChildren().add(menuVo);
                }
            }
        }
        return systemMenuVos.stream().filter(menu -> menu.getParentId() == 0).toList();
    }

    @Override
    public boolean insertMenu(SystemMenuReq systemMenuReq) {
        SystemMenu systemMenu = new SystemMenu();
        BeanUtil.copyProperties(systemMenuReq, systemMenu, "parentId", "deleted");
        systemMenu.setParentId(Integer.valueOf(systemMenuReq.getParentId()));
        systemMenu.setDeleted(Integer.valueOf(systemMenuReq.getDeleted()));
        systemMenu.setPermission(PERMISSION_PREFIX + systemMenuReq.getRouteName());
        systemMenu.setCreateBy(getSystemUserName());
        systemMenu.setCreateTime(new Date());
        return save(systemMenu);
    }

    @Override
    public boolean updateMenu(SystemMenuReq systemMenuReq) {
        SystemMenu systemMenu = new SystemMenu();
        BeanUtil.copyProperties(systemMenuReq, systemMenu, "id", "parentId", "deleted");
        systemMenu.setId(Long.valueOf(systemMenuReq.getId()));
        systemMenu.setUpdateBy(getSystemUserName());
        systemMenu.setUpdateTime(new Date());
        return updateById(systemMenu);
    }

    /**
     * @Description：查询所有菜单信息，返回集合数据
     * @Author：fengcheng
     * @Date：2025/1/17 10:21
     * @ParamsType：[]
     * @Return：java.util.List<com.example.springsecurity.entity.SystemMenu>
     */
    private List<SystemMenu> getBaseQueryWrapper() {
        LambdaQueryWrapper<SystemMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemMenu::getDeleted, DelStatusEnums.DISABLE.getCode());
        lambdaQueryWrapper.notIn(SystemMenu::getMenuType, MenuTypeEnums.BUTTON.getCode());
        return list(lambdaQueryWrapper);
    }
}
